package cn.yanglq.Rss.web.controller.mailset;

import cn.yanglq.Rss.web.common.vo.ApiResult;
import cn.yanglq.Rss.web.controller.BaseController;
import cn.yanglq.Rss.web.model.MailSet;
import cn.yanglq.Rss.web.service.MailSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * Created by yanglq on 2017/6/13.
 */
@Controller
@RequestMapping("/mailset")
public class MailSetController extends BaseController {
    @Autowired
    private MailSetService mailSetService;


    // 初始化默认分页信息
    protected int pageSize = 10;
    protected int currentNum = 1;

    private ModelAndView page(Integer pageSize, Integer currentNum, int total) {
        ModelAndView mv = new ModelAndView();
        int pageNum = (total + pageSize - 1) / pageSize;
        mv.addObject("pageSize", pageSize);
        mv.addObject("currentNum", currentNum);
        mv.addObject("pageNum", pageNum);
        mv.addObject("total", total);
        return mv;
    }

    @RequestMapping("/addMailSetPage")
    public String addMailSetPage() {
        return "/mailset/addmailset";
    }

    @RequestMapping("/mailsetpage")
    public String mailsetPage() {
        return "/mailset/mailsetlist";
    }

    @RequestMapping("/mailsetsearch")
    public ModelAndView mailsetsearch() {
//        if (null == pageSize) {
        pageSize = this.pageSize;
//        }
//        if (null == currentNum) {
        currentNum = this.currentNum;
//        }
        Date startdate = null;
        Date enddate = null;
        String Loginid = findCurrentLoginid();
        List<MailSet> list = mailSetService.selectMailInfos(Loginid);
//        int total = mailSetService.selectMailCount(Loginid, startdate, enddate);

        ModelAndView mv = new ModelAndView();
        mv.addObject("list", list);
        mv.setViewName("/mailset/mailsetsearchlist");
        return mv;
    }


    @RequestMapping("/addMailSet")
    @ResponseBody
    public ApiResult addMailSet(@RequestParam(value = "infoname", required = true) String infoname,
                                @RequestParam(value = "sendmail", required = true) String sendmail,
                                @RequestParam(value = "receivemail", required = true) String receivemail,
                                @RequestParam(value = "password", required = true) String password,
                                @RequestParam(value = "smtp", required = true) String smtp,
                                @RequestParam(value = "title", required = true) String title,
                                @RequestParam(value = "rss", required = true) String rss) {
        ApiResult result = new ApiResult();
        MailSet sendinfo = new MailSet();
        String userid = findCurrentLoginid();
        sendinfo.setInfoname(infoname);
        sendinfo.setReceivemail(receivemail);
        sendinfo.setMailpassword(password);
        sendinfo.setSendmail(sendmail);
        sendinfo.setHostname(smtp);
        sendinfo.setRssurl(rss);
        sendinfo.setTitle(title);
        sendinfo.setUserid(userid);
        sendinfo.setCreater(userid);
        sendinfo.setCreatetime(new Date());
        mailSetService.addMailInfo(sendinfo);
        result.setSuccess(true);
        result.setErrorcode(0);
        result.setMessage("新增成功！");
        return result; //校验通过返回成功
    }

    @RequestMapping("/deleteMailSet")
    @ResponseBody
    public ApiResult deleteMailSet(@RequestParam(value = "sendid", required = true) Integer sendid) {
        ApiResult result = new ApiResult();
        mailSetService.deleteMailSet(sendid);
        result.setSuccess(true);
        result.setErrorcode(0);
        result.setMessage("删除成功！");
        return result; //校验通过返回成功
    }
}
